package com.aode.utils.download.permission;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.core.app.ActivityCompat;
import androidx.core.app.AppOpsManagerCompat;
import androidx.core.content.ContextCompat;

import com.aode.utils.download.DeviceUtils;


public class KbPermissionUtils {
    private volatile  static  KbPermissionUtils instance;
    public static KbPermissionUtils getInstance(){
        if(instance == null){
            synchronized (KbPermissionUtils.class){
                if(instance==null) {
                    instance = new KbPermissionUtils();
                }
            }
        }
        return instance;
    }


    public boolean checkPermission(Context context,String... permissions){
        for(String permission : permissions){
            if(ContextCompat.checkSelfPermission(context,permission)!= PackageManager.PERMISSION_GRANTED){
                return false;
            }
        }
        for(String permission :permissions){
            String op = AppOpsManagerCompat.permissionToOp(permission);
            if(TextUtils.isEmpty(op)) {
                continue;
            }
            int result = AppOpsManagerCompat.noteProxyOp(context,op,context.getPackageName());
            if(result == AppOpsManagerCompat.MODE_IGNORED){
                return  false;
            }
            result = ContextCompat.checkSelfPermission(context,permission);
            if(result!=PackageManager.PERMISSION_GRANTED){
                return false;
            }
        }
        return true;
    }


    public void requestPermission(Activity activity,int code,String... permission){
        ActivityCompat.requestPermissions(activity,permission,code);
    }

    public static boolean needRequestPermission(){
        return Build.VERSION.SDK_INT >= 23;
    }

    public static void goSetting(final Context context){
        AlertDialog dialog = new Builder(context)
                .setTitle("提示")
                .setMessage("您已禁止了权限,是否要去开启?")
                .setPositiveButton("立即前往", (dialog12, which) -> {
                    dialog12.dismiss();
                    getAppDetailSettingIntent(context);
                })
                .setNegativeButton("以后再说", (dialog1, which) -> dialog1.dismiss())
                .create();
        dialog.setCancelable(false);
        dialog.show();
    }


    private static void getAppDetailSettingIntent(Context context){
        Intent intent = null;
        if(DeviceUtils.getManufacturer().equals("huawei")){
            intent = huaweiApi(context);
        }else if(DeviceUtils.getManufacturer().equals("xiaomi")){
            intent = xiaomiApi(context);
        }else if(DeviceUtils.getManufacturer().equals("oppo")){
            intent = vivoApi(context);
        }else if(DeviceUtils.getManufacturer().equals("samsung")){
            intent  = samsungApi(context);
        }else if(DeviceUtils.getManufacturer().equals("meizu")){
            intent =  meizuApi(context);
        }else if(DeviceUtils.getManufacturer().equals("smartisan")){
            intent = smartsanApi(context);
        }
        intent = defaultApi(context);
        context.startActivity(intent);
    }


    private static Intent defaultApi(Context context){
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

    private static Intent huaweiApi(Context context){
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            return defaultApi(context);
        }
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setComponent(new ComponentName("com.huawei.systemmanager","com.huawei.permissionmanager.ui.MainActivity"));
        return intent;
    }

    private static Intent xiaomiApi(Context context){
        Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("extra_pkgname",context.getPackageName());
        return intent;
    }

    private static Intent vivoApi(Context context){
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("packagename",context.getPackageName());
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N_MR1){
            intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.SoftPermissionDetailActivity"));
        }else{
            intent.setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.safeguard.SoftPermissionDetailActivity"));
        }
        return intent;
    }

    private static Intent oppoApi(Context context){
        return defaultApi(context);
    }

    private static Intent meizuApi(Context context){
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N_MR1){
            return defaultApi(context);
        }
        Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("packageName",context.getPackageName());
        intent.setComponent(new ComponentName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity"));
        return intent;
    }


    private static Intent smartsanApi(Context context){
        return defaultApi(context);
    }
    private static Intent samsungApi(Context context) {
        return defaultApi(context);
    }
}
